home *** CD-ROM | disk | FTP | other *** search
/ HPAVC / HPAVC CD-ROM.iso / STDERRF.ZIP / STDERRF.ASM < prev    next >
Assembly Source File  |  1995-05-29  |  12KB  |  396 lines

  1.     page 60,132
  2.     title    STDERRF - Main Procedure for the STDERRF Application
  3.     name    STDERRF
  4.     
  5. comment ÷
  6.     STDERRF                         V1.00
  7. ----------------------------------------------------------------------------
  8. NAME
  9.     STDERRF         Main Procedure for the STDERRF Application
  10.  
  11. SYNOPSIS
  12.     STDERRF Filename Exename ["Command Line"]
  13.  
  14.     where
  15.         Filename is the file that the STDERR from Exename program
  16.             is to be redirected.
  17.         Exename is the full drive:path/filename.ext of the program
  18.             that STDERRF is to execute and redirect the the
  19.             STDERR.
  20.         Command Line is an option arguement that is the command line
  21.             for the Exename application.  If there are any embedded
  22.             blanks, the entire string must be delimited by double
  23.             quotes.
  24.  
  25. DESCRIPTION
  26.     The basic outline of STDERRF is:
  27.     1.  Accepts three command line arguments:
  28.         a.    The full path and filename of the file into which STDERR is
  29.         to be written.
  30.         b.    The full path and filename of the program to be executed.
  31.         c.    The command line for the program to be executed (should be
  32.         delimited by double quotes to allow multiple arguments).
  33.         This  argument is optional.
  34.     2.  Releases all memory above the program using Int 21 function 4ah
  35.         so that there will be room enough to load and execute the
  36.         designated program.
  37.     3.  Opens the file from step #1.a above into which STDERR is to be
  38.         written.  If file exists, ask user to continue.  If to continue,
  39.         ask user if the file is to be truncated or output appended.
  40.         Perform the requested actions.
  41.     4.  Duplicates STDERR filehandle using Int 21h function 45h.
  42.     5.  Uses Int 21h function 46h, force STDERR filehandle to have the
  43.         filehandle of the opened file from step #2.
  44.     6.  Uses Int 21h function 4b00h to load and execute the program from
  45.         step #1.a.    Use the default environment and the command line from
  46.         step #1.c above.
  47.     7.  Upon return from the function 4b00h, closes the file opened in
  48.         step #2.
  49.     8.  Restores STDERR using Int 21h function 46h to force STDERR to
  50.         point to the filehandle saved from step #3 above.
  51.  
  52. PROGRAMMING NOTES
  53.     STDERRF was assembled using MASM 6.11A with the following settings:
  54.         Preserve Case of Naes in Object File
  55.         Warning Level 3
  56.         Defines: SIMPLE - See STARTUP1.ASM for explanation
  57.  
  58.     Procedures called:
  59.         Exit            EXIT.ASM
  60.         GetValidChoice        GETVLDC.ASM
  61.     DOS Interrupts:
  62.         Int 21h 3dh - Open File with Handle
  63.         Int 21h 3eh - Close File with Handle
  64.         Int 21h 40h - Write to file or device
  65.         Int 21h 42h - Move File Pointer
  66.         Int 21h 45h - Duplicate File Handle
  67.         Int 21h 46h - Force duplication of filehandle
  68.         Int 21h 4bh - Load and Execute Program
  69.         Int 21h 5bh - Create New File
  70.  
  71. MEMORY REQUIREMENTS
  72.     STDERRF.ASM
  73.         Stack:      4 bytes
  74.         Data:     32 bytes
  75.         _BSS:    128 bytes
  76.         Const:    187 bytes
  77.         Code:    314 bytes
  78.  
  79.     STDERRF Appliation
  80.         Stack:    512 bytes
  81.         Data:     83 bytes
  82.         _BSS:    128 bytes
  83.         Common:  12 bytes
  84.         Const:    760 bytes
  85.         Code:    699 bytes
  86.  
  87.     STDERRF requires 3,184 bytes of memory to exectute plus the size of
  88.     the passed environment and environment's Memory Control Block which
  89.     is 16 bytes.
  90.  
  91. RETURNS
  92.     If no errors, returns the return code of the executed program to
  93.     the Start_Up1S to be STDERRF's return code.
  94.  
  95. CAUTIONS
  96.  
  97. AUTHOR
  98.     Raymond Moon - 5 Mar 95
  99.     Copyright (c) 1995, MoonWare
  100.     ALL RIGHTS RESERVED
  101.  
  102. HISTORY
  103.     Version    - Date        - Remarks
  104.     1.00    -  5 Mar 95    - Orginal
  105.     
  106.     ÷ End of Comment
  107.     
  108.     include procesor.inc
  109. %    .MODEL    small,FORTRAN
  110.     assume    es:DGROUP
  111.  
  112. ;-----------------------------
  113. ;    Include files
  114.  
  115.     include stderrf.inc
  116.  
  117. ;=========================================================================
  118. ;    Equates
  119. ;=========================================================================
  120.  
  121. ;----------------------------
  122. ;    ARGV equates. Using the SIMPLE option, ARGV is argv[0] vice a pointer
  123. ;    to them.
  124.  
  125. argvStdErrFile    equ    ARGV
  126. argvExeFilename    equ    ARGV + 2
  127. argvCommandTail equ    ARGV + 4
  128.  
  129. ;=========================================================================
  130. ;    Structures
  131. ;=========================================================================
  132.  
  133. LPtr    struc                ; Long Pointer structure
  134. lpOffset    dw    ?        ; Offset
  135. lpSeg        dw    ?        ; Segment
  136. LPtr    ends
  137.  
  138. LoadExec_S    struc
  139. leEnvironment    dw    ?        ; Environmental block segment address
  140. leCommandTail    LPtr    <>        ; Address of Command Tail
  141. leFCB_1     LPtr    <>        ; address of default FCB, #1
  142. leFCB_2     LPtr    <>        ; address of second FCB, #2
  143. LoadExec_S    ends
  144.  
  145. CommandTail_S    struc
  146. ctLength    db    ?        ; Length of command tail minus end CR
  147. ctLeadingSpace    db    ?        ; Initial space to start command tail
  148. ctText        db    126 dup (?)    ; Command Tail
  149. CommandTail_S    ends
  150.  
  151. ;=========================================================================
  152. ;    DATA
  153. ;=========================================================================
  154.     .DATA
  155.  
  156. EmptyFCB    db        11 dup (20h), 5 dup (00h)
  157. ParamBlk    LoadExec_S    {\
  158.                 0,            ; Use STDERRF's environ
  159.                 {CommandTail, DGROUP},    ; LPTR to CommandTail
  160.                 {EmptyFCB, DGROUP},    ; LPTR to 1st FCB
  161.                 {EmptyFCB, DGROUP}}    ; LPTR to 2nd FCB
  162. DupStdErr    word        0
  163.  
  164.     .CONST
  165.  
  166. LOGO    db    "STDERRF - Redirect STDERR Output To A File Utility", CR, LF,
  167.         "Copyright (c) MoonWare, 1995", CR, LF,
  168.         "ALL RIGHTS RESERVED", CR, LF
  169.  
  170. REDIR    db    LF, "WARNING:  Redirection File Exists. Continue? (y/n): ",
  171.         BELL
  172. TRUN    db    CR, LF, "Overwrite or Append? (o/a): "
  173.  
  174.     .DATA?
  175.  
  176. CommandTail    CommandTail_S    <>
  177.  
  178. ;=========================================================================
  179. ;    CODE
  180. ;=========================================================================
  181. ;    Start CODE segment with the proper relationship with external
  182. ;    procedures
  183.  
  184.     .CODE
  185. extrn    Start_Up1s:near
  186. extrn    Exit:near
  187. extrn    GetValidChoice:near
  188.  
  189. Main    proc    ARGV:ptr, ARGC:word
  190. local    FileHandle:word
  191.  
  192. ;----------------------------
  193. ;    Display LOGO
  194.  
  195.     @WRITE    LOGO, STDERR
  196.  
  197. ;----------------------------
  198. ;    See if there is anything to do.  Must have 2 args min.    If not,
  199. ;    display usage
  200.  
  201.     cmp    ARGC, 2         ; Is ARGC == 2?
  202.     jae    MN1            ; It's equal or larger, continue
  203.     mov    ax, eqUSAGE        ; Indicate USAGE error
  204.     call    Exit            ; Call Exit
  205.  
  206. ;----------------------------
  207. ;    See if DOS version is 3.0 or later
  208.  
  209. MN1:    cmp    OSMAJOR, 3        ; Is DOS version 3.0 or better?
  210.     jae    MN2            ; Yes, continue
  211.     mov    ax, eqBADDOSVER        ; Indicate error type
  212.     call    Exit            ; Call Exit
  213.  
  214. ;----------------------------
  215. ;    Duplicate STDERR Filehandle
  216.  
  217. MN2:    mov    bx, STDERR        ; BX = handle to duplicate
  218.     mov    ah, 45h         ; Request Duplicate
  219.     int    21h            ; Call DOS
  220.     jnc    MN3            ; Successful
  221.     mov    bx, ax            ; Move DOS Error into BX
  222.     mov    ax, eqDUPERR        ; Error, tell user which error
  223.     call    Exit            ; Call Exit Routine
  224. MN3:    mov    DupStdErr, ax        ; Save Duplicated STDERR Handle
  225.  
  226. ;----------------------------
  227. ;    Initialize the Command Tail, if present.  ARGC = 3, if present.
  228.  
  229.     cmp    ARGC, 2         ; See if equal to 2 and skip
  230.     jbe    MN6            ; Yes, skip
  231.  
  232.     mov    CommandTail.ctLeadingSpace, ' '
  233.                     ; Initialize the blank
  234.  
  235.     lea    di, CommandTail.ctText    ; DI => Command Tail Text
  236.     mov    si, argvCommandTail    ; SI => Passed Command Line
  237.     mov    cx, 1            ; CX = counter, starts 1 for space
  238.  
  239. MN4:    lodsb                ; Get the next char
  240.     or    al, al            ; Is it a null?
  241.     jz    MN5            ; Yes, we are finished
  242.     stosb                ; No, store it
  243.     inc    cx            ; Account for char
  244.     jmp    MN4            ; Go start the process again
  245. MN5:    mov    byte ptr [di], CR    ; Terminate with CR
  246.     mov    CommandTail.ctLength, cl; Save length
  247.  
  248. ;----------------------------
  249. ;    Open the file into which the stderr will be directed.
  250. ;    Open first to see if it already exists.
  251.  
  252. MN6:    mov    dx, argvStdErrFile    ; DS:DX => Filename
  253.     mov    ax, 3d01h        ; Open for write only
  254.     int    21h            ; Call DOS
  255.     jnc    MN8            ; File exists, see what to do
  256.  
  257. ;----------------------------
  258. ;    The file could not be opened so it does not exist.  Create the
  259. ;    file.
  260.  
  261.     mov    dx, argvStdErrFile    ; DS:DX => filename
  262.     xor    cx, cx            ; CX =    Normal Attributes
  263.     mov    ah, 5bh         ; Request Create New File
  264.     int    21h            ; Call DOS
  265.     jnc    MN7            ; Successful, skip error
  266.     mov    bx, ax            ; Move error into BX
  267.     mov    ax, eqCREATEERR     ; Identify type of error
  268.     call    Exit            ; Call Exit Routine
  269.  
  270. MN7:    mov    FileHandle, ax        ; No Error, save filehandle
  271.     jmp    MN12            ; Skip the open handling code
  272.  
  273. ;----------------------------
  274. ;    Save the Filehandle.
  275.  
  276. MN8:    mov    FileHandle, ax
  277.  
  278. ;----------------------------
  279. ;    File exists, see if the user wants to continue.
  280.  
  281.     @WRITE    REDIR,STDERR        ; Display question
  282.  
  283.     mov    ax, "ny"        ; Pass valid choices
  284.     call    GetValidChoice        ; Get response
  285.     jnc    MN9            ; Yes, user wants to continue
  286.     mov    ax, eqUSERTERM        ; Indicate user terminate
  287.     call    EXIT            ; Call Exit
  288.  
  289. ;----------------------------
  290. ;    The user wants to continue, see if the file is to be appended or
  291. ;    truncated.
  292.  
  293. MN9:    @WRITE    TRUN, STDERR        ; Display question
  294.  
  295.     mov    ax, "ao"        ; Pass valid choices
  296.     call    GetValidChoice        ; Get user response
  297.     jnc    MN10            ; User wants to truncate
  298.  
  299. ;----------------------------
  300. ;    User wants to append, set up for a move file pointer to end of file.
  301.  
  302.     mov    ax, 4202h        ; Request move filepointer to end
  303.     jmp    MN11            ; Go to the rest of int code
  304.  
  305. ;----------------------------
  306. ;    User wants to truncate, set up for a move file pointer to start
  307.  
  308. MN10:    mov    ax, 4200h        ; Request move filepointer to start
  309.  
  310. ;----------------------------
  311. ;    Move file pointer to desired starting position
  312.  
  313. MN11:    mov    bx, FileHandle        ; BX = filehandle
  314.     xor    cx, cx            ; DX:CX = Offset
  315.     mov    dx, cx            ;   Offset = 0000:0000
  316.     int    21h            ; Call DOS
  317.  
  318. ;---------------------------
  319. ;    Write zero bytes to confirm position
  320.  
  321.     mov    dx, offset TRUN     ; DS:DX => buffer to print
  322.     xor    cx, cx            ; CX = zero length to write
  323.     mov    bx, FileHandle        ; BX = Filehandle
  324.     mov    ah, 40h         ; Request write file or device
  325.     int    21h            ; Call DOS
  326.  
  327. ;----------------------------
  328. ;    Force duplication of STDERR filehandle to FileHandle
  329.  
  330. MN12:    mov    bx, FileHandle        ; BX = new filehandle for STDERR
  331.     mov    cx, STDERR        ; CX = filehandle to change
  332.     mov    ah, 46h         ; Request force dup filehandle
  333.     int    21h            ; Call DOS
  334.     jnc    MN13            ; Error, go tell user
  335.     mov    bx, ax            ; Move error into BX
  336.     mov    ax, eqFORCEDUPERR    ; Indicate type of error
  337.     call    Exit            ; Call Exit
  338.  
  339. ;----------------------------
  340. ;    Load and execute program.
  341.  
  342. MN13:    mov    dx, argvExeFilename    ; DS:DX => Filename to execute
  343.     mov    bx, offset ParamBlk    ; ES:BX => Parameter block
  344.     mov    ax, 4b00h        ; Request load and execute
  345.     int    21h            ; Call DOS
  346.     jnc    MN14            ; No error, continue
  347.  
  348.     mov    bx, ax            ; Error, move error code into BX
  349.     mov    ax, eqEXECERR        ; Indicate the type of error
  350.     call    Exit            ; Call Exit
  351.  
  352. ;----------------------------
  353. ;    Restore STDERR by force duplicating STDERR handle back to saved
  354. ;    filehandle for STDERR
  355.  
  356. MN14:    mov    cx, DupStdErr        ; CX = new handle for STDERR
  357.     mov    bx, STDERR        ; BX = handle to change
  358.     mov    ah, 46h         ; Request force dup filehandle
  359.     int    21h            ; Call DOS
  360.     jnc    MN15            ; No error, continue
  361.  
  362.     mov    DupStdErr, 0        ; Indicate not to redirect STDERR
  363.                     ;   again
  364.     mov    bx, ax            ; Error, move error code into BX
  365.     mov    ax, eqRESTOREERR    ; Indicate the type of error
  366.     call    Exit            ; Call Exit
  367.  
  368. ;----------------------------
  369. ;    Zero DupStdErr so that after any future errors the Exit procedure
  370. ;    will not attempt to restore STDERR.
  371.  
  372. MN15:    mov    DupStdErr, 0        ; Indicate not to redirect STDERR
  373.                     ;   again
  374.  
  375. ;----------------------------
  376. ;    Close redirected file.
  377.  
  378.     mov    bx, FileHandle        ; BX = Filehande to close
  379.     mov    ah, 3eh         ; Request close filehandle
  380.     int    21h            ; Call DOS
  381.     jnc    MN16            ; No error, continue
  382.  
  383.     mov    bx, ax            ; Error, move error code into BX
  384.     mov    ax, eqCLOSEERR        ; Indicate the type of error
  385.     call    Exit            ; Call Exit
  386.  
  387. ;----------------------------
  388. ;    Return to startup code with the return code of the child program.
  389.  
  390. MN16:    mov    ah, 4dh         ; Request get child return code
  391.     int    21h            ; Call DOS
  392.     ret                ; Return to startup code to exit
  393.  
  394. Main    endp
  395.     end
  396.